<?php
class Landing extends CI_Controller {
	private $user = null;
	public function __construct()
	{
		parent:: __construct();
		//print_r($this->session->all_userdata());
		//if($this->session->userdata('loginStatus')) redirect('home');

		parse_str($_SERVER['QUERY_STRING'], $_REQUEST);
		$this->load->library('fb_api/facebook', array("appId" => $this->config->item('appId') , "secret" => $this->config->item('secret')));
		$this->user = $this->facebook->getUser();
	}

	public function index(){

		if ($this->user) {
		  try {
		    // Proceed knowing you have a logged in user who's authenticated.
		    $user_profile = $this->facebook->api('/me');
		    $fb_email = $user_profile['email'];
		    //Kiem tra xem trong CSDL da co email nay chua
		    $check_email = $this->db->select('id, fullname, email')->get_where('member', array('email' => $fb_email));
		    if($check_email->num_rows() > 0 ) {
		    	$row = $check_email->row();
		    	$sess = array(
			    	'loginStatus' => TRUE,
			    	'sess_fullname' => $user_profile['name'],
			    	'sess_memId'	=> $row->id,
			    	'log_with_fb' => TRUE
		    	);
		    	$this->session->set_userdata($sess);
		    	redirect('home');
		    } else {
		    	$data_insert = array(
		    		'email'	=> $user_profile['email'],
		    		'fullname'	=> $user_profile['name'],
		    		'status'	=> 1
		    	);
		    	$this->db->insert('member', $data_insert);
		    	$memId = $this->db->insert_id();
		    	$sess = array(
			    	//'loginStatus' => TRUE,
			    	'sess_fullname' => $user_profile['name'],
			    	'sess_memId'	=> $memId,
			    	//'log_with_fb' => TRUE
		    	);
		    	$this->session->set_userdata($sess);
		    	redirect('update-info');
		    }
		    
		    
		  } catch (FacebookApiException $e) {
		    error_log($e);
		    $user = null;
		  }
		}

		$data['loginFbUrl'] = $this->facebook->getLoginUrl(array('scope' => 'email'));
		$this->load->view('landing/index', $data);	
	}

	public function login(){
		$email = $this->input->post('email');
		$pass = md5(md5($this->input->post('password')));
		$sql = $this->db->get_where('member', array("email" => $email));
		if($sql->num_rows() === 0 ) {
			$error = array('status' => FALSE, 'message' => 'This email is not valid or not exists');	
		} else {
			$rows = $sql->row_array();
			if($pass !== $rows['password']) {
				$error = array('status' => FALSE, 'message' => 'Password is wrong');
			} else {
				if($rows['status'] == 1) {
					
					$sess = array(
				    	'loginStatus' => TRUE,
				    	'sess_fullname' => $rows['fullname'],
				    	'sess_memId' => $rows['id'],
				    	'sess_memAvatar' => $rows['avatar'],
				    	'log_with_fb' => FALSE
				    );
				    $this->session->set_userdata($sess);
					$error = array('status' => TRUE, 'message' => '');
				} else {
					$error = array('status' => FALSE, 'message' => 'Your account is not already to active');	
				}		
			}	
		}

		echo json_encode($error);
	}
}